import Vue from "vue";
import Vuex from "vuex";
import { getUserInfo } from "../api/user";
Vue.use(Vuex);
const store = new Vuex.Store({
	  state: {
		position:[],
		address:"暂无地址"
	  },
    mutations: {
        login(state, options) {
            uni.setStorage({
                key: 'authorization',
                data: options.data,
                success: () => {
                    if (options.cb) {
                        options.cb();
                    }
                }
            });
        },
        getUserInfo(state, options = {}) {
            getUserInfo().then((res) => {
                uni.setStorage({
                    key: 'userInfo',
                    data: res.data,
                    success: () => {
                        if (options.cb) {
                            options.cb(res.data);
                        }
                    }
                });
            })
			.catch((err) => {
                // uni.showToast({
                //     title: err.message,
                //     icon: 'none'
                // });
            });
        },
	
        logout(state) {
            uni.clearStorage();
        },
		getAddress(state,options){
			state.position = options
			
		},
		avatar(state, options) {
		    uni.setStorage({
		        key: 'avatar',
		        data: options,
		        success: () => {
		            if (options.cb) {
		                options.cb();
		            }
		        }
		    });
		},
		username(state, options) {
		    uni.setStorage({
		        key: 'username',
		        data: options,
		        success: () => {
		            if (options.cb) {
		                options.cb();
		            }
		        }
		    });
		},
		phone(state, options) {
		    uni.setStorage({
		        key: 'phone',
		        data: options,
		        success: () => {
		            if (options.cb) {
		                options.cb();
		            }
		        }
		    });
		},
		encrypdata(state, options) {
		    uni.setStorage({
		        key: 'encrypdata',
		        data: options,
		        success: () => {
		            if (options.cb) {
		                options.cb();
		            }
		        }
		    });
		},
		iv(state, options) {
		    uni.setStorage({
		        key: 'iv',
		        data: options,
		        success: () => {
		            if (options.cb) {
		                options.cb();
		            }
		        }
		    });
		},
    },
});

export default store;